<template>
  <div class="ls-table-search">
    <div class="ls-table-search-form">
      <div class="coustom-search-form">
        <!-- 展开表单 -->
        <div
          class=""
          style="position: absolute; z-index: 9; background: #fff; height: auto; width: auto"
        >
          <el-form
            ref="refFormRefs"
            :class="[
              'ls-form',
              {
                'is-inline': inline,
                'ls-show-all': showAll,
                'ls-show-all-border': styleType === 'border' && showAll
              },
              ''
            ]"
            inline
            :label-width="labelWidth"
            :model="model"
            v-bind="$attrs"
          >
            <el-row>
              <!-- $attrs  props 没有接收的属性 -->
              <template v-for="(item, index) in config" :key="index">
                <el-col
                  v-show="index < hiddenIndex || showAll"
                  v-if="!item.hidden"
                  class="ls-search-col-custom"
                  :class="{
                    'ls-show-col': !showAll,
                    'ls-search-col-custom-part': styleType === 'part'
                  }"
                >
                  <!-- 插槽 -->
                  <slot v-if="item.type === 'itemSlot'" :name="item.slotName" v-bind="item"></slot>
                  <!-- 二级头部 -->
                  <div v-else-if="item.type === 'header'" :key="index" class="segment-title">
                    {{ item.label }}
                  </div>
                  <!-- 隐藏 -->
                  <span
                    v-else-if="item.hide || item.hidden"
                    :key="index + 1"
                    :style="{
                      'grid-column-start': 'span 24',
                      'grid-column': 'span ' + (item.span || 24),
                      'grid-row': 'span ' + (item.row || 1),
                      display: item.hidden ? 'none' : ''
                    }"
                  ></span>
                  <!-- 普通的 -->
                  <layout
                    v-else
                    :key="index + 2"
                    class="ls-form-item"
                    :style="{
                      'grid-column-start': 'span 24',
                      'grid-column': 'span ' + (item.span || 24),
                      'grid-row': 'span ' + (item.row || 1)
                    }"
                    :item="item"
                    :model="model"
                    :global="$attrs"
                    @mapAnewValidate="anewValidate"
                  >
                    <!-- 地图走校验anewValidate -->
                    <slot
                      :name="item.slotName"
                      v-bind="{ value: model[item.prop], row: item }"
                    ></slot>
                  </layout>
                </el-col>
              </template>
            </el-row>
          </el-form>
        </div>
      </div>
      <!-- 展开按钮 -->
      <div class="ls-show-more">
        <el-buttonlink style="margin: 8px 0; display: block" type="primary"> 展开 </el-buttonlink>
      </div>
    </div>

    <!-- 操作按钮区域 -->
    <div class="ls-table-btn el-form-item">
      <slot name="searchBtn"></slot>
    </div>
  </div>
</template>
<script setup>
import { DArrowRight } from '@element-plus/icons-vue'
import layout from 'ls-form/src/layout.vue'
import { ref, onMounted, onBeforeMount } from 'vue'
import { useResizeObserver } from '@vueuse/core'
const formWidth = ref('100%')
let emit = defineEmits(['update:config', 'update:model', 'collapse'])
const width = ref(0)
const props = defineProps({
  model: Object,
  config: Array,
  inline: Boolean,
  formItemWidth: {
    type: Number,
    default: 380
  },
  labelWidth: {
    type: String,
    default: 'auto'
  },
  styleType: {
    type: String,
    default: 'border' // border  part default
  }
})
const hiddenIndex = ref(3)
// 新增的搜索样式及方法
const showAll = ref(false)
const showMore = () => {
  showAll.value = !showAll.value
  emit('collapse', showAll.value)
}
// 根据自定义class获取当前是否有隐藏元素
const showExpand = ref(true)
const cloumnWidth = ref('1fr')
const getDisplayNone = (refFormWidth) => {
  // 计算form区域宽度
  const formContentWidth = refFormWidth - 80 - 20 // 80 展开按钮占位 20是内边距
  // const width=window.innerWidth
  let domListLength = document.querySelectorAll('.ls-search-col-custom').length
  // 根据表单区域宽度判断展示多少列
  const maxColumnValue = parseInt(formContentWidth / props.formItemWidth)
  const maxColumn = maxColumnValue > 0 ? maxColumnValue : 1 // 最少要容纳一项
  let formWidthValue = maxColumn * props.formItemWidth // 计算后的表单区域宽度
  formWidth.value = formWidthValue + 20 + 'px' // 加上内边距后赋值
  domListLength > maxColumn ? (showExpand.value = true) : (showExpand.value = false)
  hiddenIndex.value = maxColumn // 隐藏的表单内容起始位置/布局列数
  cloumnWidth.value = formWidthValue / maxColumn + 'px' // 网格布局宽度
}
const rfmediaRow = ref(null)
onMounted(() => {
  useResizeObserver(rfmediaRow, (entries) => {
    const entry = entries[0]
    width.value = entry.contentRect.width
    getDisplayNone(width.value)
  })
})

//结束
const refFormRefs = ref(null)
const anewValidate = (prop) => {
  refFormRefs.value.validateField(prop)
}
const validate = (callback) => {
  refFormRefs.value.validate(callback)
}
const resetFields = () => {
  refFormRefs.value.resetFields()
}
const clearValidate = (props) => {
  refFormRefs.value.clearValidate(props)
}
const setFormConfig = (config, data, prop, attr = 'options') => {
  config.find((item) => item.prop === prop)[attr] = data
  emit('update:config', config)
}
const setFormModel = (formData, data, prop) => {
  formData[prop] = data
  emit('update:model', formData)
}
const collapse = (value = false) => {
  showAll.value = value
  emit('collapse', value)
}
defineExpose({
  anewValidate,
  validate,
  resetFields,
  clearValidate,
  setFormConfig,
  setFormModel,
  collapse
})
</script>
<style lang="scss" scoped>
.ls-table-search {
  width: 100%;
  height: 52px;
  background: #000;
  margin: 0;
  display: flex;
  .ls-table-search-form {
    flex: 1;
    height: auto;
    position: relative;
    .coustom-search-form {
      width: calc(100% - 100px);
      padding-right: 20px;
      position: relative;
      background: #ccc;
      height: 100%;
      float: left;
    }

    .ls-show-more {
      width: 100px;
      padding-left: 10px;
      height: 100%;
      overflow: hidden;
      background: #ddd;
      float: left;
    }
  }
  .ls-table-btn {
    flex-shrink: 1;
  }
}
</style>
